<template>
  <view class="config-page">
    <NavBar></NavBar>
    
    <view class="config-container">
      <view class="config-title">系统配置</view>
      
      <view class="form-group">
        <text class="form-label">服务器地址</text>
        <input 
          class="form-input"
          v-model="serverUrl"
          placeholder="示例：http://192.168.0.1:9001/api/game/wzry/"
          @input="clearError"
        />
        <text class="error-message" v-if="error">{{ error }}</text>
      </view>
      
      <view class="form-group">
        <text class="form-label">版本号</text>
        <input 
          class="form-input"
          v-model="version"
          disabled
          placeholder="版本号"
        />
      </view>
      
      <button class="save-button" @click="saveConfig">保存配置</button>
    </view>
    
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getVersion } from '@/static/js/hub';
const serverUrl = ref('');
const error = ref('');
const version = ref(''); // 从package.json获取版本号

// 页面加载时读取当前配置
onMounted(async () => {
  serverUrl.value = uni.getStorageSync('serverBaseUrl') || '';
  version.value = await getVersion();
  
});



// 清除错误信息
const clearError = () => {
  error.value = '';
};

// URL验证函数
const validateUrl = (url) => {
  if (!url) {
    return '服务器地址不能为空';
  }
  
  // 简单的URL格式验证
  const urlRegex = /^(http|https):\/\/[^\s]+$/;
  if (!urlRegex.test(url)) {
    return '请输入有效的URL格式，如http://192.168.0.1:9001/';
  }
  
  // 检查是否包含必要的API路径
  if (!url.endsWith('/')) {
    return 'URL需以斜杠(/)结尾';
  }
  
  return '';
};

// 保存配置
const saveConfig = () => {
  // 验证URL
  const validationError = validateUrl(serverUrl.value);
  if (validationError) {
    error.value = validationError;
    return;
  }
  
  try {
    // 保存到本地存储
    uni.setStorageSync('serverBaseUrl', serverUrl.value);
    
    // 显示成功提示
    uni.showToast({
      title: '配置保存成功',
      icon: 'success',
      duration: 1500
    });
    
    // 清除输入框焦点
    uni.hideKeyboard();
  } catch (e) {
    error.value = '保存失败，请稍后重试';
    console.error('保存配置失败:', e);
    
    uni.showToast({
      title: '保存失败',
      icon: 'error',
      duration: 1500
    });
  }
};
</script>

<style scoped lang="scss">
.config-page {
  min-height: 100vh;
  background-color: #f5f7fa;
  padding: 20rpx;
  box-sizing: border-box;
}

.config-container {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 40rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  margin-top: 20rpx;
  max-width: 600rpx;
  margin-left: auto;
  margin-right: auto;
}

.config-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #303133;
  margin-bottom: 30rpx;
  text-align: center;
}

.form-group {
  margin-bottom: 30rpx;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #303133;
  margin-bottom: 10rpx;
  font-weight: 500;
}

.form-input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #e4e7ed;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 26rpx;
  color: #303133;
  background-color: #fff;
  box-sizing: border-box;
  transition: border-color 0.3s;
}

.form-input:focus {
  border-color: #3c9cff;
  outline: none;
}

.form-input:disabled {
  background-color: #f5f7fa;
  color: #c0c4cc;
  border-color: #e4e7ed;
}

.error-message {
  display: block;
  font-size: 22rpx;
  color: #f56c6c;
  margin-top: 10rpx;
}

.save-button {
  width: 100%;
  height: 80rpx;
  background-color: #3c9cff;
  color: #fff;
  font-size: 28rpx;
  font-weight: 500;
  border: none;
  border-radius: 8rpx;
  margin-top: 20rpx;
  transition: background-color 0.3s;
}

.save-button:active {
  background-color: #2980b9;
}
</style>
